import http from './http'
import { apiMsgProp } from '@/utils/global'
import type { RoleItem, RoleItemCreate, ClassTeacher } from '@/types/role'

const RoleService = {
    /**
     * @function list 获取角色列表
     * @returns RoleItem[] 角色列表
     */
    list: async (): Promise<RoleItem[]> => {
        const response = await http.get('/course/role/list')

        if (response.data.code === 200) {
            return response.data.data.items
        } else {
            apiMsgProp(response.data.code)
            return Promise.reject(response.data)
        }
    },
    /**
     * @function getById 获取角色信息
     * @param id 角色id
     * @returns RoleItem 角色信息
     */
    getById: async (id: number): Promise<RoleItem> => {
        const res = await http.get(`/course/role/${id}/info`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function create 新增角色
     * @param data 角色信息
     */
    create: async (data: RoleItemCreate) => {
        const res = await http.post('/course/role/save', data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        apiMsgProp(res.data.code)
    },
    /**
     * @function update 更新角色
     * @param data 角色信息
     */
    update: async (data: RoleItemCreate) => {
        const res = await http.post('/course/role/save', data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        apiMsgProp(res.data.code)
    },
    /**
     * @function delete 删除角色
     * @param id 角色id
     */
    delete: async (id: number) => {
        const res = await http.get(`/course/role/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        apiMsgProp(res.data.code)
    },
    /**
     * @function toggle 开启/禁用角色
     * @param id 角色id
     */
    toggle: async (id: number) => {
        const res = await http.get(`/course/role/${id}/toggle`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        apiMsgProp(res.data.code)
    },
    /**
     * @function getPermissionTree 获取角色权限树
     * @param id 角色id
     * @returns 角色权限树
     */
    getPermissionTree: async (id: number) => {
        const res = await http.get(`/course/role/${id}/permission-tree`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data
    },

    /**
     * @function getByCourseId 根据课程id获取角色信息(班主任信息)
     * @param id 课程id
     * @returns 班主任信息
     */
    getByCourseId: async (id: number): Promise<ClassTeacher[]> => {
        const res = await http.get(`/course/role/course/${id}/class-teacher`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
}

export default RoleService
